define(['amazeui', 'jquery', 'global/url', 'global/constants'],
  function(amazeui, jquery, url, constants) {
    return ['$scope', '$rootScope', 'UrlService', 'AdminService', 'UtilService', 'AdminsService',
    		 function($scope, $rootScope, UrlService, AdminService, UtilService, AdminsService) {
        $scope.awesomeThings = [
	      'HTML5 Boilerplate',
	      'AngularJS',
	      'Karma'
        ];
          var params = {
               'accessToken': $scope.accessToken,
               'filter': {
                'pagesize': 10,
                'pno': 1,
               'search': ''
           }
        }
        $scope.remove = function(admin){
          var operationVo = { 
             'id':admin.adminUserId
           }, param = {
               'accessToken': $scope.accessToken,
               'operate':JSON.stringify(operationVo)
           };
           AdminsService.adminDelete(param).success(function(response, status){
                if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
                  _refreshAdmin(); 
                  UtilService.clearValidForm('#admin-add-modal');
                }
            });
        }
        $scope.addadmin = function(add){
         var operationVo = { 
            'data': {
              'userName': add.userName ,
              'firstName': add.firstName,
              'lastName': add.lastName,
              'email': add.email
            }
          }; 
            UtilService.validateForm('#admin-add-modal');
            if($('#admin-add-modal').data('amui.validator').isFormValid()) {
            var param = {
               'accessToken': $scope.accessToken,
               'operate':JSON.stringify(operationVo)
              };
             AdminsService.adminAdd(param).success(function(response, status){
                if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
                   add.email=add.lastName=add.firstName=add.userName="";
                   $('.am-close-spin').click();
                  _refreshAdmin(); 
                }
            });
          };

        }

        function _refreshAdmin () {
            params.filter.pagesize = $scope.admins.pagesize;
            params.filter.pno = $scope.admins.currentPage;
            AdminsService.adminList(params).success(function(response, status) {
                if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
                  $scope.admins = response.jsonResponse.response.data;
                } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
                    AdminService.Admin.logout();
                   window.location.href = constants.pages.login;
                }
            });
        }
        function _getAdminList (params) {
            AdminsService.adminList(params).success(function(response, status) {
                if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
                   $scope.admins = response.jsonResponse.response.data;
                } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
                   AdminService.Admin.logout();
                   window.location.href = constants.pages.login;
                }
            });
        }
         UtilService.renderSelect();
        _getAdminList(params);
       

     }];
  }
)